Python 3.12.10 (tags/v3.12.10:0cc8128, Apr  8 2025, 12:21:36) [MSC v.1943 64 bit (AMD64)] on win32
Enter "help" below or click "Help" above for more information.
>>> from datetime import datetime
... from flask_sqlalchemy import SQLAlchemy
... 
... db = SQLAlchemy()
... 
... class Product(db.Model):
...     """商品表"""
...     __tablename__ = 'products'
...     
...     id = db.Column(db.Integer, primary_key=True)
...     name = db.Column(db.String(100), nullable=False)
...     barcode = db.Column(db.String(50), unique=True)
...     category = db.Column(db.String(50))
...     purchase_price = db.Column(db.Float)
...     selling_price = db.Column(db.Float)
...     min_stock = db.Column(db.Integer, default=10)  # 库存预警阈值
...     description = db.Column(db.Text)
...     created_at = db.Column(db.DateTime, default=datetime.now)
...     
...     inventory = db.relationship('Inventory', backref='product', lazy=True)
...     batches = db.relationship('Batch', backref='product', lazy=True)
... 
... class Location(db.Model):
...     """货位表"""
...     __tablename__ = 'locations'
...     
...     id = db.Column(db.Integer, primary_key=True)
...     code = db.Column(db.String(20), unique=True, nullable=False)
...     area = db.Column(db.String(50))
...     description = db.Column(db.Text)
...     
...     inventory = db.relationship('Inventory', backref='location', lazy=True)
... 
... class Inventory(db.Model):
...     """库存表"""
...     __tablename__ = 'inventory'
...     
...     id = db.Column(db.Integer, primary_key=True)
    product_id = db.Column(db.Integer, db.ForeignKey('products.id'), nullable=False)
    location_id = db.Column(db.Integer, db.ForeignKey('locations.id'), nullable=False)
    quantity = db.Column(db.Integer, default=0)
    last_updated = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)

class Batch(db.Model):
    """批次表"""
    __tablename__ = 'batches'
    
    id = db.Column(db.Integer, primary_key=True)
    product_id = db.Column(db.Integer, db.ForeignKey('products.id'), nullable=False)
    batch_number = db.Column(db.String(50), nullable=False)
    production_date = db.Column(db.Date)
    expiry_date = db.Column(db.Date)
    quantity = db.Column(db.Integer, default=0)
    supplier = db.Column(db.String(100))
    purchase_price = db.Column(db.Float)
    arrived_at = db.Column(db.DateTime, default=datetime.now)
    
    location_id = db.Column(db.Integer, db.ForeignKey('locations.id'))
